//
//  ChoiceDetailViewController.m
//  项目
//
//  Created by 关雷 on 15-1-4.
//  Copyright (c) 2015年 张诗雨. All rights reserved.
//

#import "ChoiceDetailViewController.h"
#import "WebColor.h"
#import "AFNetWoringYu.h"
#import "ChpiceCommentViewController.h"
#import "ChoiceWriterViewController.h"
#define SCREEN_WIDTH ([UIScreen mainScreen].bounds.size.width)
#define SCREEN_HEIGHT ([UIScreen mainScreen].bounds.size.height)
#define DEFAULTS ([NSUserDefaults standardUserDefaults])
@interface ChoiceDetailViewController ()<UIWebViewDelegate, UIGestureRecognizerDelegate>

@property (nonatomic, assign) NSInteger fontState;

@property (nonatomic, assign) NSInteger praise;
@end

@implementation ChoiceDetailViewController
- (void)dealloc
{

    [_upView release];
    [_fontStateView release];
    [_contentid release];
    [_uid release];
    [_webV release];
    [_downView release];
    [_comment release];
    [_praiseNum release];
    [_shareurl release];
    [_writerView release];
    [_blackView release];
    [_shareView release];
    [super dealloc];
}
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self startConnect];
    [self createWebView];
    [self createFontState];
    [self stateView];
    [self createDownView];
    [self createTapGesture];
    [self pushwriterView];
    [self createShareView];
    self.view.backgroundColor = [UIColor whiteColor];
    // Do any additional setup after loading the view.
}
//网络请求
- (void)startConnect
{
    NSString * body = [NSString stringWithFormat:@"client=1&contentid=%@&source=app", self.contentid];
    [AFNetWoringYu netWorkWithURL:@"http://api.pianke.me/article/info" body:body resultBlock:^(id dic) {
        NSDictionary * data = [dic objectForKey:@"data"];
        NSDictionary * user = [data objectForKey:@"userinfo"];
        self.uid = [user objectForKey:@"uid"];
        NSDictionary * counter = [data objectForKey:@"counterList"];
        
        NSNumber * comment = [counter objectForKey:@"comment"];
        ///把NSNumber类型转换为NSString类型.否则会崩
        NSNumberFormatter * number = [[NSNumberFormatter alloc] init];
        self.comment = [number stringFromNumber:comment];
        NSNumber * praiseNum = [counter objectForKey:@"like"];
        self.praiseNum = [number stringFromNumber:praiseNum];
        [number release];
        self.shareurl = [data objectForKey:@"shareurl"];
//        if ([[DEFAULTS objectForKey:@"openViewNum"] isEqualToString:@"2"]) {
//            NSInteger pra = [self.praiseNum intValue];
//            NSString * praise = [NSString stringWithFormat:@"%ld", pra + 1];
//            _downView.lable1.text = praise;
//        } else {
            _downView.lable1.text = self.praiseNum;
//        }
        
        _downView.lable2.text = self.comment;
    }];
}
// 点击进入作者主页的视图
- (void)pushwriterView
{
    self.blackView = [[UIView alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH, SCREEN_HEIGHT)];
    _blackView.backgroundColor = [UIColor blackColor];
    _blackView.alpha = 0.4;
    _blackView.userInteractionEnabled = YES;
    [self.view addSubview:_blackView];
    
    self.writerView = [[UIView alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH, 80)];
    _writerView.backgroundColor = [UIColor whiteColor];
    _writerView.userInteractionEnabled = YES;
    [self.view addSubview:_writerView];
    [_writerView release];
    UIImageView * imageV = [[UIImageView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH / 2 - 90, _writerView.frame.size.height / 4 - 3, 40, 40)];
    imageV.image = [UIImage imageNamed:@"iconfont-home.png"];
    [_writerView addSubview:imageV];
    [imageV release];
    UILabel * lable = [[UILabel alloc] initWithFrame:CGRectMake(SCREEN_WIDTH / 2 - 40, _writerView.frame.size.height / 4, SCREEN_WIDTH / 2, 40)];
    [lable setText:@"查看作者主页"];
    lable.font = [UIFont systemFontOfSize:25];
    lable.textColor = [UIColor grayColor];
    [_writerView addSubview:lable];
    [lable release];
}
- (void)createShareView
{
    self.shareView = [[UIView alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH, SCREEN_HEIGHT - 100)];
    _shareView.backgroundColor = [UIColor whiteColor];
    _shareView.userInteractionEnabled = YES;
    [self.view addSubview:_shareView];
    [_shareView release];

}
//点击手势
- (void)createTapGesture
{
    UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGestureAction)];
    [self.webV addGestureRecognizer:tap];
    tap.delegate = self;
    tap.cancelsTouchesInView = NO;
    [tap release];

}
//点击的协议方法
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
    return YES;
}

//点击事件
- (void)tapGestureAction
{
    [self viewFrame];

}
- (void)viewFrame
{
    if (_upView.frame.origin.y > -5) {
        [UIView animateWithDuration:0.5 animations:^{
            self.fontStateView.frame = CGRectMake(0, -155, SCREEN_WIDTH, 150);
            self.upView.frame = CGRectMake(-2, -66, SCREEN_WIDTH + 4, 65);
            self.downView.frame = CGRectMake(-2, SCREEN_HEIGHT, SCREEN_WIDTH + 4, 55);
        }];
    } else {
        [UIView animateWithDuration:0.5 animations:^{
            
            self.upView.frame = CGRectMake(-2, -2, SCREEN_WIDTH + 4, 65);
            self.downView.frame = CGRectMake(-2, SCREEN_HEIGHT - 53, SCREEN_WIDTH + 4, 55);
        }];
    }

}
//创建webView
- (void)createWebView
{
    NSString * str = [NSString stringWithFormat:@"http://pianke.me/webview/%@", self.contentid];
    self.webV = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    _webV.backgroundColor = [UIColor whiteColor];
    NSURLRequest * request = [NSURLRequest requestWithURL:[NSURL URLWithString:str]];
    [_webV loadRequest:request];
    [self.view addSubview:_webV];
    _webV.delegate = self;
    _webV.scrollView.bounces = NO;
    _webV.userInteractionEnabled = YES;
    [_webV release];
    self.navigationController.navigationBarHidden = YES;
    if (![DEFAULTS objectForKey:@"openViewAgain"]) {
        [DEFAULTS setObject:@"100" forKey:@"numOfFont"];
    }
    
    
}
//下次进入是否黑夜模式和字体大小
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    if ([[DEFAULTS objectForKey:@"openViewAgain"] isEqualToString:@"night"]) {
        [self nightTimeState];
    }
    int numOfFont = [[DEFAULTS objectForKey:@"numOfFont"] intValue];
    NSString * str = [NSString stringWithFormat:@"document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '%d%%'", numOfFont];
    [self.webV stringByEvaluatingJavaScriptFromString:str];
}
//状态view(返回,字体(夜间模式), 作者)
- (void)stateView
{
    
    self.upView = [[CustomStateView alloc] initWithFrame:CGRectMake(-2, -2, SCREEN_WIDTH + 4, 65)];
    _upView.layer.borderWidth = 1.0;
    _upView.layer.borderColor = [[UIColor grayColor] CGColor];
    _upView.backgroundColor = [WebColor white];
    [_upView.buttonLeft1 setImage:[UIImage imageNamed:@"iconfont-fanhui.png"] forState:UIControlStateNormal];
    [_upView.buttonLeft1 addTarget:self action:@selector(popLastView) forControlEvents:UIControlEventTouchUpInside];
    
    [_upView.buttonRight1 setImage:[UIImage imageNamed:@"iconfont-mmsejingshenzhuangtai.png"] forState:UIControlStateNormal];
    [_upView.buttonRight1 addTarget:self action:@selector(startButton) forControlEvents:UIControlEventTouchUpInside];
    self.fontState = 0;
    [_upView.buttonRight2 setImage:[UIImage imageNamed:@"iconfont-pinglun.png"] forState:UIControlStateNormal];
    [_upView.buttonRight2 addTarget:self action:@selector(writerButton) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:_upView];
    [_upView release];
}

//字体(夜间模式)view
- (void)createFontState
{
    self.fontStateView = [[CustomStateView alloc] initWithFrame:CGRectMake(0, -135, SCREEN_WIDTH, 150)];
    _fontStateView.backgroundColor = [WebColor gainsboro];
    _fontStateView.buttonLeft1.frame = CGRectMake(20, 10, SCREEN_WIDTH / 2 - 35, 60);
    [_fontStateView.buttonLeft1 setImage:[UIImage imageNamed:@"iconfont-suoxiaoziti-2.png"] forState:UIControlStateNormal];
    [_fontStateView.buttonLeft1.layer setBorderWidth:2.0];
    [_fontStateView.buttonLeft1.layer setBorderColor:[[WebColor dodgerBlue] CGColor]];
    [_fontStateView.buttonLeft1 addTarget: self action:@selector(reduceFontButtonAction) forControlEvents:UIControlEventTouchUpInside];
    
    _fontStateView.buttonLeft2.frame = CGRectMake(20, 80, SCREEN_WIDTH / 2 - 35, 60);
   
    [_fontStateView.buttonLeft2 setImage:[UIImage imageNamed:@"iconfont-icon-2.png"] forState:UIControlStateNormal];
    [_fontStateView.buttonLeft2.layer setBorderWidth:2.0];
    [_fontStateView.buttonLeft2.layer setBorderColor:[[WebColor dodgerBlue] CGColor]];
    [_fontStateView.buttonLeft2 addTarget:self action:@selector(dayTimeState) forControlEvents:UIControlEventTouchUpInside];
    
    _fontStateView.buttonRight1.frame = CGRectMake(SCREEN_WIDTH / 2 + 15, 10, SCREEN_WIDTH / 2 - 35, 60);
    [_fontStateView.buttonRight1.layer setBorderWidth:2.0];
    [_fontStateView.buttonRight1.layer setBorderColor:[[WebColor dodgerBlue] CGColor]];
    [_fontStateView.buttonRight1 setImage:[UIImage imageNamed:@"iconfont-fangdaziti-2.png"] forState:UIControlStateNormal];
    [_fontStateView.buttonRight1 addTarget:self action:@selector(addFontButtonAction) forControlEvents:UIControlEventTouchUpInside];
    
    _fontStateView.buttonRight2.frame = CGRectMake(SCREEN_WIDTH / 2 + 15, 80, SCREEN_WIDTH / 2 - 35, 60);
    [_fontStateView.buttonRight2.layer setBorderWidth:2.0];
    [_fontStateView.buttonRight2.layer setBorderColor:[[WebColor dodgerBlue] CGColor]];
    [_fontStateView.buttonRight2 setImage:[UIImage imageNamed:@"iconfont-yueliang.png"] forState:UIControlStateNormal];
    _fontStateView.buttonRight2.titleLabel.textColor = [UIColor whiteColor];
    [_fontStateView.buttonRight2 addTarget:self action:@selector(nightTimeState) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_fontStateView];
    [_upView release];
}
//下面的功能视图(点赞,评论,收藏,分享)
- (void)createDownView
{
    
    self.downView = [[CustomStateView alloc] initWithFrame:CGRectMake(-2, SCREEN_HEIGHT - 53, SCREEN_WIDTH + 4, 55)];
    _downView.layer.borderWidth = 1.0;
    _downView.layer.borderColor = [[UIColor grayColor] CGColor];
    _downView.backgroundColor = [WebColor white];
    
    _downView.lable1.textColor = [UIColor grayColor];
    
    _downView.lable2.textColor = [UIColor grayColor];
    
    [_downView.buttonLeft1 setImage:[UIImage imageNamed:@"iconfont-dianzan.png"] forState:UIControlStateNormal];
    [_downView.buttonLeft1 addTarget:self action:@selector(praiseAction) forControlEvents:UIControlEventTouchUpInside];
    self.praise = -1;
    [_downView.buttonLeft2 setImage:[UIImage imageNamed:@"iconfont-pinglun.png"] forState:UIControlStateNormal];
    [_downView.buttonLeft2 addTarget:self action:@selector(commentAction) forControlEvents:UIControlEventTouchUpInside];
    
    [_downView.buttonRight1 setImage:[UIImage imageNamed:@"iconfont-shoucang.png"] forState:UIControlStateNormal];
    [_downView.buttonRight1 addTarget:self action:@selector(collectAction) forControlEvents:UIControlEventTouchUpInside];
    
    [_downView.buttonRight2 setImage:[UIImage imageNamed:@"iconfont-zhuanfa.png"] forState:UIControlStateNormal];
    [_downView.buttonRight2 addTarget:self action:@selector(shareAction) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:_downView];
    [_downView release];
    
}
//返回上一视图
- (void)popLastView
{
    [_upView.buttonLeft1 setImage:[UIImage imageNamed:@"iconfont-fanhui.png"] forState:UIControlStateHighlighted];
    [self.navigationController popViewControllerAnimated:YES];

}
//字体(夜间模式)视图的收放
- (void)startButton
{
    
    if (self.fontState == 0) {
        [UIView animateWithDuration:0.5 animations:^{
            self.fontStateView.frame = CGRectMake(0, 63, SCREEN_WIDTH, 150);
        }];
        self.fontState = 1;
    } else {
        [UIView animateWithDuration:0.5 animations:^{
            self.fontStateView.frame = CGRectMake(0, -135, SCREEN_WIDTH, 150);
        }];
        self.fontState = 0;
    }
}
//点击出现点击会出现作者主页的视图
- (void)writerButton
{
    [DEFAULTS setObject:@"writer" forKey:@"writer"];
    self.blackView.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
    [UIView animateWithDuration:0.3 animations:^{
        self.writerView.frame = CGRectMake(0, SCREEN_HEIGHT - 80, SCREEN_WIDTH, 80);
       
    }];
}
//字体放大
- (void)addFontButtonAction
{
    int numOfFont = [[DEFAULTS objectForKey:@"numOfFont"] intValue];
    if (numOfFont < 250) {
        numOfFont += 20;
        NSString * str = [NSString stringWithFormat:@"document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '%d%%'", numOfFont];
        [self.webV stringByEvaluatingJavaScriptFromString:str];
        NSString * font = [NSString stringWithFormat:@"%d", numOfFont];
        [DEFAULTS setObject:font forKey:@"numOfFont"];
    }
    
    
}
//字体缩小
- (void)reduceFontButtonAction
{
    int numOfFont = [[DEFAULTS objectForKey:@"numOfFont"] intValue];
    if (numOfFont > 60) {
        numOfFont -=20;
        NSString * str = [NSString stringWithFormat:@"document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '%d%%'", numOfFont];
        [self.webV stringByEvaluatingJavaScriptFromString:str];
        NSString * font = [NSString stringWithFormat:@"%d", numOfFont];
        [DEFAULTS setObject:font forKey:@"numOfFont"];
    }
}
//白天模式
- (void)dayTimeState
{
    [self.webV stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.background='white'"];
    [self.webV stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.color='black'"];
    _downView.backgroundColor = [UIColor whiteColor];
    _upView.backgroundColor = [UIColor whiteColor];
    [DEFAULTS setObject:@"day" forKey:@"openViewAgain"];
}
//夜间模式
- (void)nightTimeState
{
    [self.webV stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.background='#011422'"];
    [self.webV stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.color='#345566'"];
    _downView.backgroundColor = [UIColor blackColor];
    _upView.backgroundColor = [UIColor blackColor];
    [DEFAULTS setObject:@"night" forKey:@"openViewAgain"];

    
}
//点赞
- (void)praiseAction
{
    NSInteger pra = [self.praiseNum intValue];
    NSString * praise = [NSString stringWithFormat:@"%ld", pra + 1];
    if (pra > self.praise) {
        _downView.lable1.text = praise;
        
        
    } else {
        UIAlertView * alert = [[UIAlertView alloc]initWithTitle:nil message:@"亲,你已经表达过了" delegate:self cancelButtonTitle:nil otherButtonTitles:nil, nil];
//        alert.frame = CGRectMake(<#CGFloat x#>, <#CGFloat y#>, <#CGFloat width#>, <#CGFloat height#>)
        [self.view addSubview:alert];
//        [alert show];
        [alert release];
    }
    self.praise = pra;
    
}
//评论
- (void)commentAction
{
    ChpiceCommentViewController * ch = [[ChpiceCommentViewController alloc] init];
    NSInteger num = [self.comment integerValue];
    ch.contentid = self.contentid;
    ch.number = num;
    [self.navigationController pushViewController:ch animated:YES];
    [ch release];
    
}
//收藏
- (void)collectAction
{
    
}
//分享
- (void)shareAction
{
    self.blackView.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
    [UIView animateWithDuration:0.5 animations:^{
        self.shareView.frame = CGRectMake(0, 100, SCREEN_WIDTH, SCREEN_HEIGHT - 100);
    }];
    
}
//触摸事件(blackView的,upView的, downView的...)
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    //当前点击坐标
    UITouch * touch = [touches anyObject];
    CGPoint point = [touch locationInView:self.view];
    
    //writer视图显示
    if (self.writerView.frame.origin.y < SCREEN_HEIGHT - 1) {
        [DEFAULTS setObject:@"writer" forKey:@"writer"];
    }
    //share视图显示
    if (self.shareView.frame.origin.y == 100) {
        [DEFAULTS setObject:@"nowriter" forKey:@"writer"];
        self.blackView.frame = CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH, SCREEN_HEIGHT);
        [UIView animateWithDuration:0.5 animations:^{
            self.shareView.frame = CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH, SCREEN_HEIGHT - 100);
        }];
        //点击writer视图
    } else if ([[DEFAULTS objectForKey:@"writer"] isEqualToString:@"writer"] && point.y > SCREEN_HEIGHT - 80) {
        [DEFAULTS setObject:@"nowriter" forKey:@"writer"];
        ChoiceWriterViewController * writer = [[ChoiceWriterViewController alloc] init];
        [self.navigationController pushViewController:writer animated:YES];
        [writer release];
        //当writer视图显示时点击black视图收回black视图和writer视图
    } else if ([[DEFAULTS objectForKey:@"writer"] isEqualToString:@"writer"] && point.y < SCREEN_HEIGHT - 80) {
        self.blackView.frame = CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH, SCREEN_HEIGHT);
        [UIView animateWithDuration:0.3 animations:^{
            self.writerView.frame = CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH, 80);
            
        }];
        //upView视图和down视图的显示和收回
    } else {
        [self viewFrame];
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
//bar的出现
- (void)viewWillDisappear:(BOOL)animated
{
    self.navigationController.navigationBarHidden = NO;
    self.tabBarController.tabBar.hidden = NO;
}
//bar的消失
- (void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBarHidden = YES;
    self.tabBarController.tabBar.hidden = YES;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
